<template>
  <div>
    <div class="headerNav" v-show="obtainInput">
      <!-- 搜索框 -->
      <div class="header">
        <div class="left iconfont icon-geren" @click="personal"></div>
        <div class="center">MUSIC-VUE</div>
        <div class="right iconfont icon-fenxiang" @click="add"></div>
      </div>
      <!-- 搜索 -->
      <div class="search" v-show="search" @click="inputflag = true">
        <van-search
          v-model="value"
          shape="round"
          show-action
          background="#d44439"
          placeholder="请输入搜索关键词"
          @input="inputflag = false"
        >
          <template #action>
            <div @click="CancelSearch">取消</div>
          </template>
        </van-search>
        <div class="inputflag" v-show="inputflag">
          <p class="PopularSearch">热门搜索:</p>
          <ul>
            <li
              @click="app(item.first)"
              v-for="(item, index) in searchtw"
              :key="index"
            >
              <p @click="inputname(item.first)">{{ item.first }}</p>
            </li>
          </ul>
        </div>
        <div class="result" v-show="!inputflag">
          <p>搜索“{{ value }}”</p>
          <ul>
            <li
              @click="setId(item.name)"
              v-for="(item, index) in suggest"
              :key="index"
              v-show="index < 10"
            >
              <label class="iconfont icon-fenxiang"></label
              ><span>{{ item.name }}</span>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <!-- 搜索内容 -->
    <div class="obtain" v-show="!obtainInput">
      <div v-for="(item, index) in searchtw" :key="index">
        <img :src="item.artist" alt="" /><span>{{ item.name }}</span>
      </div>
    </div>
  </div>
</template>
<script>
import {
  getSearchHot,
  getSearchSuggest,
  // getSongDetail,
} from "../api/recommend";
export default {
  data() {
    return {
      search: false,
      searchtw: null,
      suggest: null,
      inputflag: true,
      value: "",
      obtainInput: true,
      topsong: null,
    };
  },
  methods: {
    personal() {
      this.$router.push("/userinfo");
    },
    add() {
      this.search = true;
    },
    app(first) {
      this.value = first;
    },
    focusSearch() {
      getSearchSuggest({ keywords: this.value }).then((data) => {
        this.suggest = data.result.songs;
      });
    },
    CancelSearch() {
      this.$router.go("0");
    },
    inputname(item) {
      this.value = item;
    },
    setId(name) {
      this.$router.push({ name: "setID", query: { name: name } });
    },
  },
  created() {
    getSearchHot().then((data) => {
      this.searchtw = data.result.hots;
    });
  },
  watch: {
    value() {
      this.focusSearch();
    },
  },
};
</script>
<style lang="less">
.headerNav {
  width: 100%;
  height: 100%;
  //头部
  .header {
    width: 100%;
    position: fixed;
    top: 0px;
    background-color: #d44439;
    color: #fff;
    height: 39px;
    line-height: 39px;
    display: flex;
    flex-wrap: wrap;
    text-align: center;
    z-index: 10;
    .left,
    .right {
      flex: 0 0 60px;
      font-size: 18px;
    }
    .left {
      font-size: 25px;
    }
    .center {
      flex: 1;
      input {
        width: 90%;
        height: 22px;
        border-radius: 10px;
        border: none;
      }
    }
    //导航
    .navs {
      height: 40px;
      line-height: 40px;
      display: flex;
      color: #fff;
      .item {
        flex: 1;
        text-align: center;
        font-size: 14px;
        span {
          display: inline-block;
          height: 21px;
          line-height: 21px;
          border-bottom: 1px solid transparent;
        }
        &.router-link-active span {
          border-color: #fff;
          font-weight: bold;
        }
      }
    }
  }

  //搜索
  .search {
    width: 100vw;
    height: 100vh;
    background-color: white;
    position: fixed;
    top: 0px;
    left: 0px;
    z-index: 10;
    .inputflag {
      .PopularSearch {
        padding: 10px 5px;
        font-size: 16px;
        color: black;
        font-weight: bold;
      }
      ul {
        overflow: hidden;
        padding: 0 5px;
        display: flex;
        flex-wrap: wrap;
        font-size: 12px;
        li {
          margin: 5px 5px;
          p {
            padding: 2px 10px;
            border-radius: 20px;
            border: 1px solid #ccc;
          }
        }
      }
    }
    .result {
      p {
        padding: 10px 5px;
        font-size: 14px;
        color: #507daf;
        border-bottom: 1px solid #ccc;
      }
      ul {
        width: 97%;
        margin: 0 auto;
        li {
          padding: 10px 0;
          width: 100%;
          font-size: 14px;
          overflow: hidden;
          white-space: nowrap;
          span {
            margin-left: 10px;
            display: inline-block;
            width: 100%;
            border-bottom: 1px solid #ccc;
          }
        }
      }
    }
    .col {
      color: white;
      font-weight: bold;
    }
  }
}
//搜索内容
.obtain {
  width: 100%;
  height: 100vh;
  background-color: #fff;
}
</style>